// 获取应用实例
const app = getApp()
import { date_time } from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    inventoryValues: {
      Id: 0,
      Details: [], // 库存列表
      UseStates: [], // 资产状态
    }, // 盘点信息
    inventoryDetailView: {
      Unit: '',
      InventoryAmount: 0, // 盘点数量
      AssetsPrice: 0,
      AssetsQuantity: 0,
    }, // 库存信息
    showView: false, // 查看框
    thumbUrl: '/assets/box.png', // 卡片默认图片
    filter: {
      PDState: 1, // 状态
      PDStates: [{ text: '全部', value: 0 },
      { text: '已盘点', value: 1 },
      { text: '盘盈', value: 2 },
      { text: '盘亏', value: 3 },
      { text: '未盘点', value: 9 },],
      sorter: 'CardNo', // 状态
      Sorters: [{ text: '默认排序', value: 'CardNo' },
      { text: '日期排序', value: 'CreatedOn' },],
      SearchCardNo: '', // 卡片编号
      SearchName: '', // 资产名称
      SearchOrganizationName: '', // 单位名称
      SearchDepartmentName: '', // 部门名称
    },
    pageIndex: 0, // 当前页Index
    totalPages: 1, // 明细页数
  },

  loadInventory: function () {
    const _this = this;
    const { id, filter: { PDState, SearchCardNo, SearchName, SearchOrganizationName, SearchDepartmentName, sorter } } = _this.data;
    app.ajax({
      url: 'ERP/InventoryView',
      data: { id, PDState, SearchCardNo, SearchName, SearchOrganizationName, SearchDepartmentName, sorter },
      verifyToken: true,
      success(data) {
        if (data.result) {
          const { model = [], pageIndex = 0, totalPages = 1 } = data;
          _this.setData({ inventoryValues: model, pageIndex, totalPages });
        }
        else {
          app.showNotify(data.msg);
          return;
        }
      }
    });
  },

  loadInventoryDetails: function (push = false) {
    const _this = this;
    const { id, filter: { PDState, SearchCardNo, SearchName, SearchOrganizationName, SearchDepartmentName, sorter }, inventoryValues, pageIndex: i, totalPages: t } = _this.data;
    if (push && i + 1 >= t) return;
    app.ajax({
      url: 'ERP/InventoryDetailsList',
      data: { id, PDState, SearchCardNo, SearchName, SearchOrganizationName, SearchDepartmentName, sorter, pageIndex: push ? i + 1 : i },
      verifyToken: true,
      success(data) {
        if (data.result) {
          const { model = [], pageIndex = 0, totalPages = 1 } = data;
          inventoryValues.Details = push ? [...inventoryValues.Details, ...model || []] : model || [];
          _this.setData({ inventoryValues, pageIndex, totalPages });
        }
        else {
          app.showNotify(data.msg);
          return;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { id } = options;
    this.setData({ id });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadInventory();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadInventoryDetails(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  stop() {
    console.log("进入后台");
  },

  done(e) {
    console.log("初始化完成", e);
  },

  error(e) {
    app.showNotify('用户未授权');
  },

  onPDStateChange({ detail }) {
    const _this = this;
    const { filter } = _this.data;
    filter.PDState = detail;
    _this.setData({ filter, pageIndex: 0, totalPages: 1 });
    _this.loadInventoryDetails();
  },

  onSorterChange({ detail }) {
    const _this = this;
    const { filter } = _this.data;
    filter.sorter = detail;
    _this.setData({ filter, pageIndex: 0, totalPages: 1 });
    _this.loadInventoryDetails();
  },

  // 根据筛选条件查询
  onSearch() {
    const _this = this;
    _this.selectComponent('#item').toggle();
    _this.setData({ pageIndex: 0, totalPages: 1 });
    _this.loadInventoryDetails();
  },

  // 输入框值变化
  onFieldChange(e) {
    const _this = this;
    const { filter } = _this.data;
    const { detail, target: { id } } = e;
    filter[id] = detail;
    this.setData({ filter });
  },

  goAdd(e) {
    const _this = this;
    const { id } = _this.data;
    const { target: { dataset: { opt } } } = e;
    wx.navigateTo({
      url: 'add?inventoryId=' + id + '&opt=' + opt,
    })
  },

  goScan(e) {
    const _this = this;
    const { id } = _this.data;
    const { target: { dataset: { opt } } } = e;
    wx.navigateTo({
      url: 'scan?inventoryId=' + id + '&opt=' + opt,
    })
  },

  showView(e) {
    const _this = this;
    const { id: inventoryId } = _this.data;
    const { currentTarget: { id } } = e;
    app.ajax({
      url: 'ERP/GetMaterialsInventoryView', // 获取资产盘点信息
      data: { id, inventoryId },
      verifyToken: true,
      success(data) {
        if (data.result) {
          const inventory = data.inventory || {};
          if (inventory.StartOn) {
            inventory.StartOn = date_time(inventory.StartOn);
          }
          if (inventory.CreatedOn) {
            inventory.CreatedOn = date_time(inventory.CreatedOn);
          }
          _this.setData({ showView: true, inventoryDetailView: inventory });
        }
        else {
          app.showNotify(data.msg);
        }
      }
    });
  },

  closeView(e) {
    this.setData({ showView: false });
  },

  edit(e) {
    const _this = this;
    const { inventoryDetailView: { Id, FixedAssetsCheckId } } = _this.data;
    _this.setData({ showView: false });
    wx.navigateTo({
      url: 'add?id=' + Id + '&inventoryId=' + FixedAssetsCheckId,
    })
  },

  delete(e) {
    const _this = this;
    const { inventoryDetailView: { Id } } = _this.data;
    app.ajax({
      url: 'ERP/DeleteMaterialsInventory', // 删除资产盘点信息
      data: { id: Id },
      verifyToken: true,
      success(data) {
        if (data.result) {
          _this.loadInventory();
          _this.setData({ showView: false });
        }
        else {
          app.showNotify(data.msg);
        }
      }
    });
  },
})